package com.w08e.waai.dto;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.time.LocalDateTime;

/**
 * AI交易策略响应DTO
 * @Author: 智能交易策略系统
 * @Date: 2025/1/14
 */
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class TradingStrategyResponse {
    
    /**
     * 策略生成时间
     */
    private LocalDateTime generatedAt;
    
    /**
     * 交易对（如：ETHUSDT）
     */
    private String symbol;
    
    /**
     * 短期策略（1-4小时）
     */
    private ShortTermStrategy shortTermStrategy;
    
    /**
     * 长期策略（1-7天）
     */
    private LongTermStrategy longTermStrategy;
    
    /**
     * 仓位管理建议
     */
    private PositionManagement positionManagement;
    
    /**
     * 风险提示
     */
    private String riskWarning;
    
    /**
     * AI原始响应内容
     */
    private String rawAiResponse;
    
    @Data
    @Builder
    @NoArgsConstructor
    @AllArgsConstructor
    public static class ShortTermStrategy {
        /**
         * 交易方向：LONG/SHORT/HOLD
         */
        private String direction;
        
        /**
         * 进场价格
         */
        private Double entryPrice;
        
        /**
         * 止盈价格
         */
        private Double takeProfitPrice;
        
        /**
         * 止损价格
         */
        private Double stopLossPrice;
        
        /**
         * 策略理由
         */
        private String reason;
        
        /**
         * 时间框架
         */
        private String timeFrame;
    }
    
    @Data
    @Builder
    @NoArgsConstructor
    @AllArgsConstructor
    public static class LongTermStrategy {
        /**
         * 交易方向：LONG/SHORT/HOLD
         */
        private String direction;
        
        /**
         * 进场价格
         */
        private Double entryPrice;
        
        /**
         * 止盈价格
         */
        private Double takeProfitPrice;
        
        /**
         * 止损价格
         */
        private Double stopLossPrice;
        
        /**
         * 策略理由
         */
        private String reason;
        
        /**
         * 时间框架
         */
        private String timeFrame;
    }
    
    @Data
    @Builder
    @NoArgsConstructor
    @AllArgsConstructor
    public static class PositionManagement {
        /**
         * 推荐仓位比例（%）
         */
        private Double positionSizePercent;
        
        /**
         * 推荐杠杆倍数
         */
        private Integer leverageMultiplier;
        
        /**
         * 资金管理建议
         */
        private String fundManagementAdvice;
    }
    
    /**
     * 格式化输出用于钉钉消息
     */
    public String formatForDingMessage() {
        StringBuilder sb = new StringBuilder();
        sb.append("🤖 AI交易策略分析报告\n");
        sb.append("━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\n");
        sb.append("📊 交易对：").append(symbol).append("\n");
        sb.append("⏰ 生成时间：").append(generatedAt).append("\n\n");
        
        if (shortTermStrategy != null) {
            sb.append("⚡ 短期策略（15分钟-4小时）\n");
            sb.append("方向：").append(getDirectionEmoji(shortTermStrategy.direction)).append(shortTermStrategy.direction).append("\n");
            sb.append("进场价：").append(shortTermStrategy.entryPrice).append("\n");
            sb.append("止盈价：").append(shortTermStrategy.takeProfitPrice).append("\n");
            sb.append("止损价：").append(shortTermStrategy.stopLossPrice).append("\n");
            sb.append("理由：").append(shortTermStrategy.reason).append("\n\n");
        }
        
        if (longTermStrategy != null) {
            sb.append("📈 长期策略（1-7天）\n");
            sb.append("方向：").append(getDirectionEmoji(longTermStrategy.direction)).append(longTermStrategy.direction).append("\n");
            sb.append("进场价：").append(longTermStrategy.entryPrice).append("\n");
            sb.append("止盈价：").append(longTermStrategy.takeProfitPrice).append("\n");
            sb.append("止损价：").append(longTermStrategy.stopLossPrice).append("\n");
            sb.append("理由：").append(longTermStrategy.reason).append("\n\n");
        }
        
        if (positionManagement != null) {
            sb.append("💰 仓位管理\n");
            sb.append("仓位比例：").append(positionManagement.positionSizePercent).append("%\n");
            sb.append("建议杠杆：").append(positionManagement.leverageMultiplier).append("x\n");
            sb.append("资金管理：").append(positionManagement.fundManagementAdvice).append("\n\n");
        }
        
        if (riskWarning != null && !riskWarning.isEmpty()) {
            sb.append("⚠️ 风险提示\n");
            sb.append(riskWarning).append("\n\n");
        }
        
        sb.append("━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\n");
        sb.append("🔔 此为AI策略建议，请结合市场情况谨慎决策\n");
        sb.append("📝 投资有风险，入市需谨慎");
        
        return sb.toString();
    }
    
    private String getDirectionEmoji(String direction) {
        if (direction == null) return "";
        switch (direction.toUpperCase()) {
            case "LONG": return "🟢 ";
            case "SHORT": return "🔴 ";
            case "HOLD": return "🟡 ";
            default: return "";
        }
    }
} 